using System;
using System.Collections;
using System.IO;
using System.Xml;

namespace FridayShopping.DataModel
{

	/// <summary>
	/// Store the data of a shop. This includes the name and the categories keys this shop
	/// keeps. The order of the category keys is also the route of the shopper.
	/// </summary>
	public class Shop : ItemBase
	{
		private ArrayList categories;

		#region Constructors
		public Shop()
		{
		}

		public Shop(string name) : base (name)
		{
		}
		#endregion

		#region Member Access
		public ArrayList Categories
		{
			get
			{
				if (categories == null) 
				{
					categories = new ArrayList();
				}
				return categories;
			}
			set 
			{
				categories = value;
			}
		}
		#endregion

		#region Add/Remove/Move up/down category keys
		public void Add(Key key)
		{
			Categories.Add(key);
		}

		public void Delete(Key key)
		{
			Categories.Remove(key);
		}

		public void MoveUp(Key key)
		{
			int index = Categories.IndexOf(key);
			if (index < 0) 
			{
				throw new ItemException("Item was not found");
			}
			// No where to move up already topmost
			if (index == 0) 
			{
				return;
			}
			Categories.RemoveAt(index);
			Categories.Insert(index-1, key);
		}

		public void MoveDown(Key key)
		{
			int index = Categories.IndexOf(key);
			if (index < 0) 
			{
				throw new ItemException("Item was not found");
			}
			// No where to move down already at bottom
			if (index == Categories.Count - 1) 
			{
				return;
			}
			Categories.RemoveAt(index);
			Categories.Insert(index + 1, key);
		}
		#endregion

		#region Serialization / Deserialization
		protected override void SerializeMembers(XmlTextWriter xmlWriter)
		{
			base.SerializeMembers (xmlWriter);

			xmlWriter.WriteStartElement("CategoryKeys");

			foreach (Key key in Categories)
			{
				key.Serialize(xmlWriter);
			}

			xmlWriter.WriteEndElement();
		}

		public override void DeserializeMembers(XmlNodeReader xmlReader)
		{
			base.DeserializeMembers (xmlReader);
			// Read category keys
			xmlReader.ReadStartElement();
			// Do we have categories?
			if (xmlReader.IsStartElement("Key")) 
			{
				while (xmlReader.IsStartElement("Key"))
				{
					Key key = Key.DeserializeMembers(xmlReader);
					Categories.Add(key);
				}
				xmlReader.ReadEndElement();
			} 

		}
		#endregion

		#region Create categories based on Shop
		public Categories CreateShoppingList()
		{
			// Create new categories 
			Categories newList = new Categories();

			foreach (Key key in Categories)
			{
				Category category = (Category)Database.Instance.Categories.GetItem(key);
				if (category != null) 
				{
					Category newCat = new Category(category.Name);
					foreach (Product product in category.Products.List) 
					{
						if (product.PurchaseComplete == false) 
						{
							newCat.Products.AddWithKey(product);
						}
					}
					newList.AddWithKey(newCat);
				}
			}
			return newList;
		}
		#endregion

	}

	/// <summary>
	/// Stores a list of shops. Each item in list is of the type 'Shop'
	/// </summary>
	/// 
	[ListItemTypeAttribute(typeof(Shop))]
	public class Shops : ItemListBase
	{
		#region Constructor
		public Shops()
		{
		}
		#endregion
	}
}
